package com.pktechan.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.ProductBaseInfo;
import com.pktechan.manage.entity.ProductChannelFee;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.service.ProductChannelFeeService;
import com.pktechan.manage.service.ProductService;
import com.pktechan.manage.utils.FastDfsHttpUtil;
import com.pktechan.manage.utils.FileResponseData;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.utils.SystemConstants;
import com.pktechan.manage.vo.ProductVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 产品相关功能
 * @author: qx.zhang
 * @create: 2021-03-05 11:27
 **/
@Controller
@RequestMapping("productController")
public class ProductController {

    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductChannelFeeService productChannelFeeService;

    @RequestMapping("/test")
    public String test() {
        return "index";
    }

    @RequestMapping("/index")
    public String index() {
        return "admin/index";
    }

    @RequestMapping("Product")
    public Integer addProduct(String name) {
        ProductBaseInfo productBaseInfo = new ProductBaseInfo();
        productBaseInfo.setName("VIP");
        productBaseInfo.setPrice(BigDecimal.TEN);
//        productService.save(productBaseInfo);
//         productService.update(productBaseInfo);
//         productService.delete(productBaseInfo);
//         productService.deleteById(1);
        productService.getById(1);
        return 1;
    }

    /**
     * 添加到产品信息
     * @param name

     * @param thumbImagePath
     * @param response
     */
    @RequestMapping(value = "saveProduct", method = RequestMethod.POST)
    public void saveProduct(@RequestParam String name, @RequestParam String detail, @RequestParam String unit, @RequestParam Integer custVipType, @RequestParam Integer category,
                            @RequestParam String vendor, @RequestParam BigDecimal price, @RequestParam BigDecimal cloudPoint, @RequestParam Integer cloudTicket,
                            @RequestParam(required = false, value = "thumbImagePath") MultipartFile thumbImagePath,@RequestParam Integer sortOrder, HttpServletResponse response) {
        try {
            logger.info("添加到产品信息");
            ProductBaseInfo productBaseInfo = new ProductBaseInfo();
            if(thumbImagePath != null){
                logger.info("上传FastDFS产品头像");
                FileResponseData fileResponseData = FastDfsHttpUtil.uploadFile(thumbImagePath, name);
                productBaseInfo.setThumbImagePath(fileResponseData.getFilePath());
                productBaseInfo.setThumbImageUrl(fileResponseData.getHttpUrl());
            }
            productBaseInfo.setName(name);
            productBaseInfo.setDetail(detail);
            productBaseInfo.setUnit(unit);
            productBaseInfo.setCustVipType(custVipType);
            productBaseInfo.setCategory(category);
            productBaseInfo.setVendor(vendor);
            productBaseInfo.setPrice(price);
//            productBaseInfo.setPriceCarriage(priceCarriage);
            productBaseInfo.setStatus(SystemConstants.YesOrNo.NO.getICode());
            productBaseInfo.setCloudPoint(cloudPoint);
            productBaseInfo.setCloudTicket(cloudTicket);
            productBaseInfo.setSortOrder(sortOrder);
            productService.save(productBaseInfo);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    /**
     * 修改产品信息

     * @param response
     */
    @RequestMapping(value = "updateProduct", method = RequestMethod.POST)
    public void updateProduct(@RequestParam Integer productId,@RequestParam String name, @RequestParam String detail, @RequestParam String unit, @RequestParam Integer custVipType,
                              @RequestParam Integer category,@RequestParam String vendor, @RequestParam BigDecimal price, @RequestParam BigDecimal cloudPoint,@RequestParam Integer cloudTicket,
                              @RequestParam(required = false, value = "thumbImagePath") MultipartFile thumbImagePath,@RequestParam Integer sortOrder, HttpServletResponse response) {
        try {
            logger.info("修改产品信息 id = "+ productId);
            ProductBaseInfo productBaseInfo = new ProductBaseInfo();
            if(thumbImagePath != null){
                logger.info("上传FastDFS产品头像");
                FileResponseData fileResponseData = FastDfsHttpUtil.uploadFile(thumbImagePath, name);
                productBaseInfo.setThumbImagePath(fileResponseData.getFilePath());
                productBaseInfo.setThumbImageUrl(fileResponseData.getHttpUrl());
            }
            productBaseInfo.setName(name);
            productBaseInfo.setDetail(detail);
            productBaseInfo.setUnit(unit);
            productBaseInfo.setCustVipType(custVipType);
            productBaseInfo.setCategory(category);
            productBaseInfo.setVendor(vendor);
            productBaseInfo.setPrice(price);
//            productBaseInfo.setPriceCarriage(priceCarriage);
            productBaseInfo.setCloudPoint(cloudPoint);
            productBaseInfo.setCloudTicket(cloudTicket);
            productBaseInfo.setId(productId);
            productBaseInfo.setSortOrder(sortOrder);
            productService.update(productBaseInfo);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    @RequestMapping("getProductById")
    public void getProductById(@RequestParam Integer productId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + productId + "查询产品信息");
            ProductBaseInfo productBaseInfo = productService.getById(productId);
            HtmlUtil.writerSuccessJson(response, productBaseInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    @RequestMapping("deleteById")
    public void deleteById(@RequestParam Integer productId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + productId + "删除产品信息");
            productService.deleteById(productId);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 分页查询List
     *
     * @param productBaseInfo
     * @return
     */
    @RequestMapping("getProductList")
    public void getProductList(@RequestBody ProductBaseInfo productBaseInfo, HttpServletResponse response) {
        try {
            logger.info("查询产品列表");
            PageHelper.startPage(productBaseInfo.getPageNo(), productBaseInfo.getPageSize());
            List<ProductBaseInfo> list = productService.getList(productBaseInfo);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到产品营销渠道费
     *
     * @param productChannelFee
     * @param response
     */
    @RequestMapping(value = "saveProductChannelFee", method = RequestMethod.POST)
    public void saveProductChannelFee(@RequestBody ProductChannelFee productChannelFee, HttpServletResponse response) {
        try {
            logger.info("添加到产品营销渠道费");
            productChannelFee.setStatus(SystemConstants.YesOrNo.YES.getICode());//默认为有效
            productChannelFeeService.saveProductChannelFee(productChannelFee);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 修改产品营销渠道费
     *
     * @param productChannelFee
     * @param response
     */
    @RequestMapping(value = "updateProductChannelFee", method = RequestMethod.POST)
    public void updateProductChannelFee(@RequestBody ProductChannelFee productChannelFee, HttpServletResponse response) {
        try {
            logger.info("修改产品营销渠道费 id = "+ productChannelFee.getId());
            productChannelFeeService.updateProductChannelFee(productChannelFee);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据id查询产品营销渠道费
     * @param id
     * @param response
     */
    @RequestMapping("getProductChannelFeeById")
    public void getProductChannelFeeById(@RequestParam Integer id, HttpServletResponse response) {
        try {
            logger.info("根据id=" + id + "查询产品营销渠道费");
            ProductVO productChannelFee = productChannelFeeService.getProductChannelFeeById(id);
            HtmlUtil.writerSuccessJson(response, productChannelFee);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 根据id删除产品营销渠道费
     * @param id
     * @param response
     */
    @RequestMapping("deleteProductChannelFeeById")
    public void deleteProductChannelFeeById(@RequestParam Integer id, HttpServletResponse response) {
        try {
            logger.info("根据id=" + id + "删除产品营销渠道费");
            productChannelFeeService.deleteById(id);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 分页查询List
     *
     * @param productVO
     * @return
     */
    @RequestMapping("getProductChannelFeeList")
    public void getProductChannelFeeList(@RequestBody ProductVO productVO, HttpServletResponse response) {
        try {
            logger.info("查询产品营销渠道费列表");
            PageHelper.startPage(productVO.getPageNo(), productVO.getPageSize());
            List<ProductVO> list = productChannelFeeService.getProductChannelFeeList(productVO);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 打开产品列表页面
     * @return
     */
    @RequestMapping("toProductList")
    public String toCloudPointFlowVoList() {
        return "product/productList";
    }
    /**
     * 打开修改产品页面
     * @return
     */
    @RequestMapping("toUpdateProduct")
    public String toUpdateProduct() {
        return "product/updateProduct";
    }
    /**
     * 打开添加产品页面
     * @return
     */
    @RequestMapping("toSaveProduct")
    public String toSaveProduct() {
        return "product/saveProduct";
    }
    /**
     * 打开产品营销渠道列表页面
     * @return
     */
    @RequestMapping("toProductChannelFeeList")
    public String toProductChannelFeeList() {
        return "product/productChannelFeeList";
    }
    /**
     * 打开修改产品营销渠道费页面
     * @return
     */
    @RequestMapping("toUpdateProductChannelFee")
    public String toUpdateProductChannelFee() {
        return "product/updateProductChannelFee";
    }
    /**
     * 打开添加产品营销渠道费页面
     * @return
     */
    @RequestMapping("toSaveProductChannelFee")
    public String toSaveProductChannelFee() {
        return "product/saveProductChannelFee";
    }

    /**
     * 修改产品信息(上下架)
     * @param productBaseInfo
     * @param response
     */
    @RequestMapping(value = "updateProductStatusById", method = RequestMethod.POST)
    public void updateProductStatusById(@RequestBody ProductBaseInfo productBaseInfo, HttpServletResponse response) {
        try {
            logger.info("修改产品信息id = "+ productBaseInfo.getId()+"上下架");
            productService.update(productBaseInfo);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 打开产品详情页面
     * @return
     */
    @RequestMapping("toProductDetail")
    public String toProductDetail() {
        return "product/productDetail";
    }

    /**
     * 修改产品置顶
     * @param productId
     * @param response
     */
    @RequestMapping(value = "updateProductTop", method = RequestMethod.POST)
    public void updateProductTop(@RequestParam Integer productId, HttpServletResponse response) {
        try {
            logger.info("修改产品信息id = "+ productId+"上下架");
            productService.updateProductTop(productId);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
}
